package dao;

import entity.Info;
import util.DbUtil;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class InfoDao {
// 增加 
	DbUtil db = new DbUtil();


//删除
	public int del(int id) {
		String sql = "DELETE FROM info WHERE id =?";
		int ok = db.executeUpdate(sql, id);
		db.closeAll();
		return ok;
	}

//修改
	public int update(Info info) {
		String sql = "UPDATE info SET name=?,phone=?,message=? WHERE id = ?";
		int ok = db.executeUpdate(sql, info.getName(), info.getPhone(), info.getMessage(), info.getId());
		db.closeAll();
		return ok;
	}

//查总数
	public int count() {
		String sql = "SELECT COUNT(*) FROM info";
		ResultSet rs = db.executeQuery(sql);
		int ok = 0;
		try {
			if (rs.next()) {
				ok = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		db.closeAll();
		return ok;
	}

//查页码
	public List<Info> getPage(int page) {
		List<Info> list = new ArrayList<>();
		int num = (page - 1) * 5;
		String sql = "SELECT * FROM info LIMIT ?,5";
		ResultSet rs = db.executeQuery(sql, num);
		try {
			while (rs.next()) {
				int id = rs.getInt(1);
				String name = rs.getString(2);
				String phone = rs.getString(3);
				String message = rs.getString(4);
				Info info = new Info(id, name, phone, message);
				list.add(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		db.closeAll();
		return list;

	}
	public boolean login(String name, String pass) {
	boolean ok= false;
	String sql="SELECT * FROM users WHERE uname=? AND upass=?";
			ResultSet rs = db.executeQuery(sql, name,pass);
	try {
		if (rs.next()) {
			ok=true;
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	db.closeAll();
	return ok;
	}
}

